// OSR b JVCS

package org.jvcs.common;

import org.jvcs.client.core.*;
import org.jvcs.common.*;
import org.jvcs.fmwk.*;

import java.io.*;
import java.net.*;
import java.util.*;

// Begin program
public class osrBuilder {


	
	public String serverHost = null;
	public int serverPort = G.DEFAULT_PORT;
	
	
	public void createFileSystemUser() {
		if (CmdLine.verboseFlag) {
			osr.pl("Create File System User");
		}
	}
	
	public void createFileSystemHub() {
		if (CmdLine.verboseFlag) {
			osr.pl("Create File System Hub");
			osr.pl("Check the filesystem and any existing hub");
			osr.pl("Check the parent path");
			osr.pl("Check the root dir");
			osr.pl("Create the structure");
			osr.pl("Confirm the status");
		}
	}
	public void createNetworkHub(String serverHost, int serverPort, String hubDetails) {
		if (CmdLine.verboseFlag) {
			osr.pl("Create Network System Hub");
			osr.pl("Create a Thread for Command Dialog");
			osr.pl("Send the URL and command for hub creation");
			osr.pl("Wait for the Details on creation");
			osr.pl("Confirm the User's request on creation");
		}
		
		// TEST1
		// after prototype take this slice and put in a class
		Socket jps = null;
		PrintWriter out = null;
		BufferedReader in = null;
		jvcsProtocol jp = null;
		jp = new jvcsProtocol();
		

        try {
            jps = new Socket(serverHost, serverPort);		
            out = new PrintWriter(jps.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(jps.getInputStream()));
        } catch (UnknownHostException e) {
            osr.ee("Don't know about host: .");
        } catch (IOException e) {
           osr.ee("Couldn't get I/O for the connection to: .");
        }

        //BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer = null;;
        String fromUser = null;
		try {
        while ((fromServer = in.readLine()) != null) {
            osr.pl("Server: " + fromServer);
			
			// Break the communication, on signal bye
            if (fromServer.equals(jp.Q_BYE))
                break;
				
			// First hand-shake on Hello say Hi
		    if (fromServer.equals(jp.Q_HELLO)) {
				fromUser = jp.Q_HI;
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}	
			
			// START COMMUNICATION
			if (fromServer.equals(jp.Q_SEND_AUTH_DETAILS)) {
				fromUser = jp.Q_AUTH_DETAILS+G.T_fs+"osr,osr";
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			if (fromServer.equals(jp.Q_AUTH_SUCCESS)) {
				fromUser = jp.Q_CREATE_HUB;
				osr.pl(">>>>>>"+hubDetails);
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			if (fromServer.startsWith(jp.Q_FILE_UPLOAD_PORT)) {
				fromUser = jp.Q_UPLOADING_FILE;
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			
			if (fromServer.startsWith(jp.Q_UPLOAD_FILE_NOW)) {
					String [] tmp = fromServer.split(G.T_fs);
					int sPortNo = Integer.parseInt(tmp[1]);
					osr.pl("CHECKING: "+tmp[0]+" "+sPortNo);
						try {
							Socket fileSocket = new Socket("localhost", sPortNo);
							String filePath = "c:/temp/TEST-SERVER.txt";
							final String largeFile = filePath;
							FileInputStream fileInputStream = new FileInputStream(largeFile);
							OutputStream socketOutputStream = fileSocket.getOutputStream();
							long startTime = System.currentTimeMillis();
							byte[] buffer = new byte[G.BUFFER_SIZE];
							int read;
							int readTotal = 0;
							while ((read = fileInputStream.read(buffer)) != -1) {
								socketOutputStream.write(buffer, 0, read);
								readTotal += read;
							}
							socketOutputStream.close();
							fileInputStream.close();
										
							long endTime = System.currentTimeMillis();
							System.out.println(readTotal + " bytes written in " + (endTime - startTime) + " ms.");
							fileSocket.close();
                        } catch (Exception e) {
							System.out.println("Error: Client File Transfer: "+e.toString());
                        }
				
				fromUser = jp.Q_FILE_WRITTEN;								
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			if (fromServer.equals(jp.Q_HUB_CREATED)) {
				fromUser = jp.Q_BYE;								
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			if (fromServer.equals(jp.Q_HUB_CREATION_FAILURE)) {
				fromUser = jp.Q_BYE;								
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
            // END COMMUNICATION
        }
		
			out.close();
			in.close();
			//stdIn.close();
			jps.close();
		} catch (Exception e1) {
			osr.ee("jps socket close: "+e1.toString());
		}
	}
	
	public void createNetworkProject(String serverHost, int serverPort, String projectDetails) {
		if (CmdLine.verboseFlag) {
			osr.pl("Create Network Hub Project");
			osr.pl("Create a Thread for Command Dialog");
			osr.pl("Send the URL and command for hub creation");
			osr.pl("Wait for the Details on creation");
			osr.pl("Confirm the User's request on creation");
		}
		
			// TEST1
		// after prototype take this slice and put in a class
		Socket jps = null;
		PrintWriter out = null;
		BufferedReader in = null;
		jvcsProtocol jp = null;
		jp = new jvcsProtocol();
		

        try {
            jps = new Socket(serverHost, serverPort);		
            out = new PrintWriter(jps.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(jps.getInputStream()));
        } catch (UnknownHostException e) {
            osr.ee("Don't know about host: .");
        } catch (IOException e) {
           osr.ee("Couldn't get I/O for the connection to: .");
        }

        //BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer = null;;
        String fromUser = null;
		try {
        while ((fromServer = in.readLine()) != null) {
            osr.pl("Server: " + fromServer);
			
			// Break the communication, on signal bye
            if (fromServer.equals(jp.Q_BYE))
                break;
				
			// First hand-shake on Hello say Hi
		    if (fromServer.equals(jp.Q_HELLO)) {
				fromUser = jp.Q_HI;
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}	
			
			// START COMMUNICATION
			if (fromServer.startsWith(jp.Q_SEND_AUTH_DETAILS)) {
				fromUser = jp.Q_AUTH_DETAILS+G.T_fs+"osr,osr";
				osr.pl(">>>>>>"+projectDetails);
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			if (fromServer.startsWith(jp.Q_AUTH_SUCCESS)) {
				fromUser = jp.Q_CREATE_PROJECT;
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			if (fromServer.startsWith(jp.Q_PROJECT_CREATED)) {
				fromUser = jp.Q_BYE;
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			if (fromServer.startsWith(jp.Q_FILE_TRANSFER_PORT)) {
				fromUser = jp.Q_DOWNLOADING_FILE;
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			
			
			if (fromServer.startsWith(jp.Q_DELIVERING_NOW)) {
					String [] tmp = fromServer.split(G.T_fs);
					int sPortNo = Integer.parseInt(tmp[1]);
					osr.pl("CHECKING: "+tmp[0]+" "+sPortNo);
						try {
								Socket fileSocket = new Socket("localhost", sPortNo);	
								long fileTime = System.currentTimeMillis();
								File f=new File("c:/temp/test1-"+fileTime+".txt");
								OutputStream outFile =new FileOutputStream(f);
								InputStream clientInputStream = null;
                                long startTime = System.currentTimeMillis();
                                byte[] buffer = new byte[Global.BUFFER_SIZE];
                                int read;
                                int totalRead = 0;
                                clientInputStream = fileSocket.getInputStream();
                                while ((read = clientInputStream.read(buffer)) != -1) {
                                        totalRead += read;
										 outFile.write(buffer,0,read);
                                }
								clientInputStream.close();
								fileSocket.close();
								outFile.close();
                                long endTime = System.currentTimeMillis();
                                System.out.println(totalRead + " bytes read in " + (endTime - startTime) + " ms.");
                        } catch (Exception e) {
							System.out.println("Error: Client File Transfer: "+e.toString());
                        }
				
				fromUser = jp.Q_FILE_WRITTEN;								
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			
			if (fromServer.startsWith(jp.Q_PROJECT_CREATION_FAILURE)) {
				fromUser = jp.Q_BYE;								
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
            // END COMMUNICATION
        }
		
			out.close();
			in.close();
			//stdIn.close();
			jps.close();
		} catch (Exception e1) {
			osr.ee("jps socket close: "+e1.toString());
		}
	}

	public void createNetworkUser(String serverHost, int serverPort, String userDetails) {
		if (CmdLine.verboseFlag) {
			osr.pl("Create Network System User");
		}
		
		// TEST1
		// after prototype take this slice and put in a class
		Socket jps = null;
		PrintWriter out = null;
		BufferedReader in = null;
		jvcsProtocol jp = null;
		jp = new jvcsProtocol();
		

        try {
            jps = new Socket(serverHost, serverPort);		
            out = new PrintWriter(jps.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(jps.getInputStream()));
        } catch (UnknownHostException e) {
            osr.ee("Don't know about host: .");
        } catch (IOException e) {
           osr.ee("Couldn't get I/O for the connection to: .");
        }

        //BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer = null;;
        String fromUser = null;
		try {
        while ((fromServer = in.readLine()) != null) {
            osr.pl("Server: " + fromServer);
			
			// Break the communication, on signal bye
            if (fromServer.equals(jp.Q_BYE))
                break;
				
			// First hand-shake on Hello say Hi
		    if (fromServer.equals(jp.Q_HELLO)) {
				fromUser = jp.Q_HI;
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}	
			
			// START COMMUNICATION
			if (fromServer.equals(jp.Q_SEND_AUTH_DETAILS)) {
				fromUser = jp.Q_AUTH_DETAILS+G.T_fs+"osr,osr";
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			if (fromServer.equals(jp.Q_AUTH_SUCCESS)) {
				fromUser = jp.Q_CREATE_USER;
				osr.pl(">>>>>>"+userDetails);
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			if (fromServer.equals(jp.Q_USER_CREATED)) {
				fromUser = jp.Q_BYE;								
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
			if (fromServer.equals(jp.Q_USER_CREATION_FAILURE)) {
				fromUser = jp.Q_BYE;								
				osr.pl("Client: "+fromUser);
				out.println(fromUser);
			}
            // END COMMUNICATION
        }
		
			out.close();
			in.close();
			//stdIn.close();
			jps.close();
		} catch (Exception e1) {
			osr.ee("jps socket close: "+e1.toString());
		}
	}
	
	
	// START THE COMM-PREREQ
	public void startComm () {
	
	}

}
// End Program